package com.llkj.qingnianguoxue.adapter;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.bean.JingXuanBean;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;
import com.llkj.qingnianguoxue.util.StringUtils;

import java.util.List;

/**
 * 首页——精选视频书籍
 */
public class HomeJingXuanAdapter extends BaseQuickAdapter<JingXuanBean,BaseViewHolder> {
    private int type = 1;//1视频，2书籍

    public HomeJingXuanAdapter(int layoutResId, List<JingXuanBean> data) {
        super(layoutResId, data);
    }

    @Override
    public int getItemCount() {
        int count = 0;
        if(mData!=null){
            if(type==2){
                count = mData.size()>4?4:mData.size();
            }else{
                count = mData.size()>3?3:mData.size();
            }
        }
        return count;
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, JingXuanBean item) {
        ImageView ivPic = baseViewHolder.getView(R.id.iv_video_pic);
        ImageLoadingUtil.loadImgGlideRoundCenterCrop(ivPic, item.getImg());
        baseViewHolder.setText(R.id.tv_title, item.getTitle());

        float priceVideo = 0f;
        try {
            priceVideo = Float.parseFloat(item.getPrice());
        }catch (Exception e){}

        if("1".equals(item.getIs_order())){
            baseViewHolder.setText(R.id.tv_price, "");
        }else{
            if(priceVideo==0){
                baseViewHolder.setText(R.id.tv_price, "免费");
            }else{
                baseViewHolder.setText(R.id.tv_price, "￥" + priceVideo);
            }
        }

        ImageView ivVideoPlay = baseViewHolder.getView(R.id.iv_play);
        TextView tvTitle = baseViewHolder.getView(R.id.tv_title);

//        TextView tvTime = baseViewHolder.getView(R.id.tv_time_long);
//        tvTime.setText(item.getDuration());
//        tvTime.setVisibility(View.INVISIBLE);

        TextView tvCatoryTag = baseViewHolder.getView(R.id.tvTag);


        LinearLayout llRoot = baseViewHolder.getView(R.id.ll_root);
        LinearLayout.LayoutParams rootParams = (LinearLayout.LayoutParams) llRoot.getLayoutParams();
        FrameLayout.LayoutParams picParams = (FrameLayout.LayoutParams) ivPic.getLayoutParams();
        if(type==2){//图书
            tvTitle.setMaxLines(1);
            ivVideoPlay.setVisibility(View.INVISIBLE);
            picParams.width = (DisplayUtil.getDensityWdith(mContext)-DisplayUtil.dip2px(mContext, 60))/4;
            picParams.height = picParams.width*14 / 13;

            if(!StringUtils.isEmpty(item.getBooksclassify())){
                tvCatoryTag.setText(item.getBooksclassify());
                tvCatoryTag.setVisibility(View.VISIBLE);
            }else{
                tvCatoryTag.setVisibility(View.INVISIBLE);
            }
        }else{
            tvTitle.setMaxLines(1);
            ivVideoPlay.setVisibility(View.VISIBLE);
            picParams.width = (DisplayUtil.getDensityWdith(mContext)-DisplayUtil.dip2px(mContext, 50))/3;
            picParams.height = picParams.width*9 / 16;

            if(!StringUtils.isEmpty(item.getClassify())){
                tvCatoryTag.setText(item.getClassify());
                tvCatoryTag.setVisibility(View.VISIBLE);
            }else{
                tvCatoryTag.setVisibility(View.INVISIBLE);
            }
        }
        rootParams.width = picParams.width;
    }

    public void setType(int type1){
        type = type1;
    }
}
